﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Data.SqlTypes;

namespace NCKH.Personal
{
    public partial class DexuatDT : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ConfigCKEditor();
            if (!IsPostBack)
            {
                hienLoaihinh();
                hienLinhvuc();
                hienCapdetai();
            }
        }
        protected void Detai_Insert()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Detai_Insert", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaDT",1);
                    Cmd.Parameters.AddWithValue("@TenDT",txtTenDT.Text);
                    Cmd.Parameters.AddWithValue("@Noidung",ckeNoidung.Text);
                    Cmd.Parameters.AddWithValue("@Muctieu",ckeMuctieu.Text);
                    Cmd.Parameters.AddWithValue("@Trangthai","Đề xuất mới");
                    Cmd.Parameters.AddWithValue("@MaLV",ddlLinhvuc.SelectedValue);
                    Cmd.Parameters.AddWithValue("@MacapDT",ddlCap.SelectedValue);
                    Cmd.Parameters.AddWithValue("@MaLH",ddlLoaihinh.SelectedValue);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void Dexuat_Insert()
        {
             string filePath = fileupLink.PostedFile.FileName;
            string filename = Path.GetFileName(filePath);
            string ext = Path.GetExtension(filename);
            string contenttype = String.Empty;
            Byte[] bytes;
            switch (ext)
            {
                case ".doc":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".docx":
                    contenttype = "application/vnd.ms-word";
                    break;
                case ".xls":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".xlsx":
                    contenttype = "application/vnd.ms-excel";
                    break;
                case ".jpg":
                    contenttype = "image/jpg";
                    break;
                case ".png":
                    contenttype = "image/png";
                    break;
                case ".gif":
                    contenttype = "image/gif";
                    break;
                case ".pdf":
                    contenttype = "application/pdf";
                    break;
            }
            if (contenttype != String.Empty)
            {

                Stream fs = fileupLink.PostedFile.InputStream;
                BinaryReader br = new BinaryReader(fs);
                bytes = br.ReadBytes((Int32)fs.Length);
            }
            else
            {
                filename=String.Empty;
                bytes = new byte[0];
            }

            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Dexuat_Insert", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@NgayDX",DateTime.Now);
                    Cmd.Parameters.AddWithValue("@Noidungchinh",ckeNoidung.Text);
                    Cmd.Parameters.AddWithValue("@Ketquadukien",ckeKetqua.Text);
                    Cmd.Parameters.AddWithValue("@Linktailieunguon",filename);
                    Cmd.Parameters.AddWithValue("@Kinhphidukien",txtKinhphi.Text);
                    Cmd.Parameters.AddWithValue("@Thoigiandukien",txtThoigian.Text);
                    Cmd.Parameters.AddWithValue("@Dulieulink",bytes);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();
                }
            }
        }
        protected void hienCapdetai()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MacapDT, TencapDT From PHANCAPDETAI";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlCap.Items.Add(
                                new ListItem(rd["TencapDT"].ToString(), rd["MacapDT"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }
        protected void hienLinhvuc()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MaLV, TenLV From LOAILINHVUC";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlLinhvuc.Items.Add(
                                new ListItem(rd["TenLV"].ToString(), rd["MaLV"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }
        protected void hienLoaihinh()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string sqlDonvi_Select = "Select MaLH, TenLH From LOAIHINH";
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand(sqlDonvi_Select, Cnn))
                {
                    Cmd.CommandType = CommandType.Text;
                    Cnn.Open();
                    using (SqlDataReader rd = Cmd.ExecuteReader())
                    {
                        while (rd.HasRows && rd.Read())
                        {
                            ddlLoaihinh.Items.Add(
                                new ListItem(rd["TenLH"].ToString(), rd["MaLH"].ToString()));
                        }
                        rd.Close();

                    }
                    Cnn.Close();
                }
            }
        }
        protected void ConfigCKEditor()
        {
            ckeMuctieu.config.toolbar = new object[] { new object[] { "Bold", "Italic","Underline", "-","Subscript", "Superscript", "NumberedList", "BulletedList", "-", "Link", "Unlink", "-", "About" }, 
                new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" }, };
            ckeNoidung.config.toolbar = new object[] { new object[] { "Bold", "Italic","Underline", "-","Subscript", "Superscript", "NumberedList", "BulletedList", "-", "Link", "Unlink", "-", "About" }, 
                new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" }, };
            ckeKetqua.config.toolbar = new object[] { new object[] { "Bold", "Italic","Underline", "-","Subscript", "Superscript", "NumberedList", "BulletedList", "-", "Link", "Unlink", "-", "About" }, 
                new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print", "SpellChecker", "Scayt" }, }; 
        }

        protected void btnGui_Click(object sender, EventArgs e)
        {
            Dexuat_Insert();
            Detai_Insert();
        }
        
    }
}